<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典显示值" prop="dictLabel">
        <el-input
          v-model="queryParams.dictLabel"
          placeholder="请输入字典显示值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典值" prop="dictValue">
        <el-input
          v-model="queryParams.dictValue"
          placeholder="请输入字典值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="dictList"
      row-key="dictId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="字典显示值" align="left" prop="dictLabel" />
      <el-table-column label="字典值" align="center" prop="dictValue" />
      <el-table-column label="状态" align="center" prop="dictStatus" >
        <template slot-scope="scope">
          {{scope.row.dictStatus===0?'启用':'禁用'}}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="排序值" prop="dictSort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dict:dict:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['dict:dict:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dict:dict:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改树形结构字典对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :destroy-on-close="true" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典显示值" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入字典显示值" />
        </el-form-item>
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入字典值" />
        </el-form-item>
        <el-form-item label="状态" prop="dictStatus">
          <el-select v-model="form.dictStatus" filterable clearable  placeholder="请选择" style="width: 380px">
            <el-option
              v-for="(item, key) in dictStatus"
              :key="key"
              :value="item.dictValue"
              :label="item.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="父级id" prop="parentId">
          <treeselect v-model="form.parentId" :options="dictOptions" :normalizer="normalizer" placeholder="请选择父级id" />
        </el-form-item>
        <el-form-item label="排序值" prop="dictSort">
          <el-input v-model="form.dictSort" placeholder="请输入排序值" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--分页-->
    <p-paging @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currPage='currPage' :pageSize='pageSize' :total='total'/>
  </div>
</template>

<script>
import { listDict, getDict, delDict, addDict, updateDict, dictChildDetail } from '@/api/dict/dict'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import pPaging from '@/components/paging.vue'

export default {
  name: 'Dict',
  components: {
    Treeselect,
    pPaging
  },
  data () {
    return {
      // 开启状态
      dictStatus: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 树形结构字典表格数据
      dictList: [],
      // 树形结构字典树选项
      dictOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      total: 0,
      currPage: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        dictSort: null,
        dictLabel: null,
        dictValue: null,
        dictType: null,
        dictStatus: null,
        parentId: null,
        ancestors: null,
        pageNum: '',
        pageSize: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created () {
    // 业务类型
    const dictStatus = 'dictStatus'
    dictChildDetail(dictStatus).then((res) => {
      let dictStatus = this.handleDict(res.data)
      dictStatus.forEach(d => {
        d.dictValue = d.dictValue - 0
        this.dictStatus.push(d)
      })
      console.log(this.dictStatus)
    })

    this.getList()
  },
  methods: {
    /** 查询树形结构字典列表 */
    getList () {
      this.loading = true
      this.queryParams.pageNum = this.currPage
      this.queryParams.pageSize = this.pageSize
      listDict(this.queryParams).then(response => {
        this.dictList = response.data.list
        this.loading = false
        this.total = response.data.total
      })
    },
    /** 转换树形结构字典数据结构 */
    normalizer (node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.dictId,
        label: node.dictLabel,
        children: node.children
      }
    },
    /** 查询树形结构字典下拉树结构 */
    getTreeselect () {
      listDict(this.queryParams).then(response => {
        this.dictOptions = []
        const data = { dictId: 0, dictLabel: '顶级节点', children: [] }
        data.children = response.data.list
        this.dictOptions.push(data)
        console.log(data)
        this.total = response.data.total
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        dictId: null,
        dictSort: null,
        dictLabel: null,
        dictValue: null,
        dictType: null,
        dictStatus: 0,

        parentId: null,
        ancestors: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.dictId) {
        this.form.parentId = row.dictId
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加树形结构字典'
    },
    /** 展开/折叠操作 */
    toggleExpandAll () {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.dictId
      }
      getDict(row.dictId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改树形结构字典'
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.dictId != null) {
            updateDict(this.form).then(response => {
              this.$notify.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDict(this.form).then(response => {
              this.$notify.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      delDict(row.dictId).then(() => {
        this.getList()
        this.$notify.success('删除成功')
      }).catch(() => {})
    },
    handleSizeChange (val1) {
      // 改变每页显示数量重新请求数据，重置当前页为第一页
      this.pageSize = val1
      this.currPage = 1
      this.getList()
    },
    handleCurrentChange (val) {
      // 点击改变当前页
      this.currPage = val
      this.getList()
    }
  }
}
</script>
